
/* Generated by Interface Builder */

#import "MaView.h"
#import <dpsclient/wraps.h>
#include <math.h>
#include <stdlib.h>
#import <dpsclient/dpsclient.h>

@implementation MaView

#define		DESSINE	1
#define		EFFACE		0


-initFrame:(const NXRect *) framerect
{
	[super initFrame:framerect];
	action = EFFACE ;
	return self;
}

- (int) dessine:(int)nombre:(int)nbrIter:(int)coul:(float ) p:(float *)abscisses: (float *)ordonnees
{
	int k,m,mg;
	float xx[NBRMAX],yy[NBRMAX];
	if( nombre == 0 ) 
		return 0 ;
	couleur = coul;
	pas_de_calcul = p;
	nombre_de_souris = nombre ;
	if (nbrIter>NBRITER)
		nombre_iterations = NBRITER ;
	else 	nombre_iterations = nbrIter;
	for( m=0 ; m<nombre ; m++ )
		{
		souris[m][0].x = abscisses[m];
		souris[m][0].y = ordonnees[m];
		}
	for( k=0 ; k<nbrIter ; k++ )
		{
		for (m=0;m<nombre;m++)
			{
			mg = (m+1) % nombre;
			xx[m] = souris[m][k].x+p*(souris[mg][k].x-souris[m][k].x);
			yy[m] = souris[m][k].y+p*(souris[mg][k].y-souris[m][k].y);
			}
		for (m=0;m<nombre;m++)
			{
			souris[m][k+1].x = xx[m];
			souris[m][k+1].y = yy[m];
			}	
		}
	action = DESSINE ;
	[self display];	
	return nombre;
}

- efface:sender
{
	action = EFFACE ;
	nombre_de_souris = 0;
	[self display];
	return self;
}

-drawSelf:(NXRect *)rects :(int)rectCount
{
	float maxX , maxY ;
	float minX , minY ;
	float coefX , coefY ;
	int i,k;
	float X,Y,Z,inverse;
	float rougeDebut, vertDebut, bleuDebut;
	float rougeFin, vertFin, bleuFin;
	float r,v,b;
	
	NXConvertColorToRGB([couleurFond color],&rougeFin,&vertFin,&bleuFin);
	PSsetrgbcolor(rougeFin, vertFin, bleuFin);
	NXRectFill(&bounds);
	if( action == DESSINE )
		{
		maxX = minX = souris[0][0].x ;
		maxY = minY = souris[0][0].y ;
		for (k=0;k<nombre_de_souris;k++)
			{
			for( i=0 ; i<nombre_iterations ; i++ )
				{
				if( souris[k][i].x < minX ) minX = souris[k][i].x ;
					else if( souris[k][i].x > maxX )  maxX= souris[k][i].x ;
				if( souris[k][i].y < minY ) minY = souris[k][i].y ;
					else if( souris[k][i].y > maxY ) maxY = souris[k][i].y ;
				}
			}
			
		if (fabs(coefX = fabs(maxX - minX)) < 1e-6) coefX = 1.0;
		if (fabs(coefY = fabs(maxY - minY)) < 1e-6) coefY = 1.0;

		printf("%f,%f\n", NX_WIDTH(&bounds) ,NX_HEIGHT(&bounds));
		
		NXConvertColorToRGB([couleurFin color],&rougeFin,&vertFin,&bleuFin);
		NXConvertColorToRGB([couleurDebut color],&rougeDebut,&vertDebut,&bleuDebut);
		r = (rougeFin-rougeDebut)/nombre_iterations;
		v = (vertFin-vertDebut)/nombre_iterations;
		b = (bleuFin-bleuDebut)/nombre_iterations;
		inverse = 1.0/(float)nombre_iterations;

		PSsetlinewidth(1.0);
		for (k=0;k<nombre_de_souris;k++)
			{
			X = (souris[k][0].x-minX)* NX_WIDTH(&bounds) / coefX;
			Y = (souris[k][0].y-minY)* NX_HEIGHT(&bounds) / coefY;
			for( i=1 ; i<nombre_iterations ; i++)
				{
				switch(couleur)
				{
				case 1:	Z = 1.0-pow((float)i*inverse,0.166666666);
						PSsetgray(Z);
						break;
				case 2:	Z = pow((float)i*inverse,3.0);
						PSsetgray(Z);
						break;
				case 3 :	PSsetrgbcolor(rougeDebut+r*i,vertDebut+v*i,bleuDebut+b*i);
						break;
				default:	PSsetgray(NX_WHITE);
				}
				PSnewpath();		
				PSmoveto(X,Y);
				X = (souris[k][i].x-minX)* NX_WIDTH(&bounds) / coefX;
				Y = (souris[k][i].y-minY)* NX_HEIGHT(&bounds) / coefY;
				PSlineto( X , Y ) ;
				PSstroke();	
				}
			}
		}
	else
		{
		}	
	
	return self;
}


@end
